package com.xgc.book.bookserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgc.book.bookserver.bean.*;
import com.xgc.book.bookserver.mapper.BookMapper;
import com.xgc.book.bookserver.service.*;
import com.xgc.book.bookserver.util.ResultJson;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 图书表 服务实现类
 * </p>
 *
 * @author wrz
 * @since 2023-11-20
 */
@Service
@Transactional
public class BookServiceImpl extends ServiceImpl<BookMapper, BookBean> implements BookService {

    @Autowired
    private BookCategoryService bookCategoryService;
    @Autowired
    private BookImageService bookImageService;
    @Autowired
    private BookCatalogService bookCatalogService;
    @Autowired
    private BookCommentService bookCommentService;
    @Autowired
    private BookLikeService bookLikeService;

    @Override
    public ResultJson getPage(int page, int limit, BookBean bookBean) {
        IPage<BookBean> iPage = this.page(new Page<BookBean>(page, limit),
                new QueryWrapper<BookBean>()
                        .eq(StringUtils.isNotEmpty(bookBean.getBookName()), "book_name", bookBean.getBookName())
                        .eq(StringUtils.isNotEmpty(bookBean.getAuthorName()), "author_name", bookBean.getAuthorName())
                        .eq(bookBean.getAuthorId() != null, "author_id", bookBean.getAuthorId())
                        .eq(bookBean.getBookStatus() != null, "book_status", bookBean.getBookStatus())
                        .eq(bookBean.getStatus() != null, "status", bookBean.getStatus())
                        .orderByDesc("book_id"));
        return ResultJson.ok(iPage);
    }

    @Override
    public void update(BookBean bookBean) {
        if (bookBean.getBookId() != null) {
            bookCategoryService.remove(new QueryWrapper<BookCategoryBean>().eq("book_id", bookBean.getBookId()));
            bookImageService.remove(new QueryWrapper<BookImageBean>().eq("book_id", bookBean.getBookId()));
        }
        this.saveOrUpdate(bookBean);
        /*处理图书分类*/
        List<BookCategoryBean> bcList = new ArrayList<>();
        if (bookBean.getCategories() != null) {
            for (Integer cid : bookBean.getCategories()) {
                BookCategoryBean bc = new BookCategoryBean();
                bc.setBookId(bookBean.getBookId());
                bc.setCategoryId(cid);
                bcList.add(bc);
            }
            bookCategoryService.saveBatch(bcList);
        }
        /*处理图书图片*/
        List<BookImageBean> imageBeanList = new ArrayList<>();
        if (bookBean.getImages() != null) {
            for (String image : bookBean.getImages()) {
                BookImageBean bookImageBean = new BookImageBean();
                bookImageBean.setBookId(bookBean.getBookId());
                bookImageBean.setImageSrc(image);
                imageBeanList.add(bookImageBean);
            }
            bookImageService.saveBatch(imageBeanList);
        }
    }

    @Override
    public void delete(int bookId) {
        this.removeById(bookId);
        bookCatalogService.remove(new QueryWrapper<BookCatalogBean>().eq("book_id", bookId));
        bookCategoryService.remove(new QueryWrapper<BookCategoryBean>().eq("book_id", bookId));
        bookCommentService.remove(new QueryWrapper<BookCommentBean>().eq("book_id", bookId));
        bookImageService.remove(new QueryWrapper<BookImageBean>().eq("book_id", bookId));
        bookLikeService.remove(new QueryWrapper<BookLikeBean>().eq("book_id", bookId));
    }

    @Override
    public ResultJson hotList() {
        List<BookBean> list = this.list(new QueryWrapper<BookBean>()
                .eq("status", 1)
                .orderByDesc("score_count")
                .last("limit 6"));
        return ResultJson.ok(list);
    }
}
